/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.spreadsheet.eval;

import cn.ac.nercis.pes.community.component.spreadsheet.PesSheetCellType;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import org.controlsfx.control.spreadsheet.GridBase;
import org.controlsfx.control.spreadsheet.SpreadsheetCell;
import org.controlsfx.control.spreadsheet.SpreadsheetCellType;
import org.controlsfx.control.spreadsheet.SpreadsheetView;

import java.util.Arrays;

/**
 * 测评结果记录表调研
 * @author zpy
 * @version 1.0.0
 */
public class ManageEvalSheetView extends SpreadsheetView {
    private final static Integer DATA_COUNT = 5;

    private final ObservableList<String> rList;

    public ManageEvalSheetView(){
        this.getStyleClass().add("pes-sheet-view");
        rList = FXCollections.observableArrayList("符合","部分符合","不符合","不适用");

        var grid = new GridBase(DATA_COUNT, 6);
        buildGrid(grid);
        setGrid(grid);

        getColumns().get(0).setPrefWidth(170);
        getColumns().get(1).setPrefWidth(120);
        getColumns().get(2).setPrefWidth(350);
        getColumns().get(3).setPrefWidth(200);
        getColumns().get(4).setMinWidth(100);
        getColumns().get(5).setMinWidth(110);

        grid.spanRow(4,1,1);
    }

    protected void buildGrid(GridBase grid){
        ObservableList<ObservableList<SpreadsheetCell>> rows = FXCollections.observableArrayList();
        generateColumnData(rows);
        generateTestData1(rows);
        generateTestData2(rows);
        generateTestData3(rows);
        generateTestData4(rows);
        grid.setRows(rows);
    }

    private void generateColumnData(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> title1 = FXCollections.observableArrayList();
        title1.addAll(generateTitleCell("测评指标",0,0),
                generateTitleCell("测评对象",0,1),
                generateTitleCell("结果记录",0,2),
                generateTitleCell("证据文件",0,3),
                generateTitleCell("测评指标符合情况（符合/部分符合/不符合/不适用）",0,4),
                generateTitleCell("测评单元得分",0,5));
        rows.add(title1);
    }

    private void generateTestData1(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("了解并遵守密码相关法律法规和密码管理制度",1,0),
                generateDataCell("人员管理（包括安全管理制度类文档、记录表单类文档、系统相关人员）",1,1,false),
                generateTextAreaDataCell("经访谈系统负责人及系统相关人员，系统相关人员了解并遵守密码相关法律法规和密码应用安全管理制度。",1,2),
                generateEvidenceDataCell(1),
                generateComboBoxDataCell(1,4,rList,"符合"),
                generateDataCell("1",1,5,false));
        rows.add(data);
    }

    private void generateTestData2(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("建立密码应用岗位责任制度",2,0),
                generateDataCell("人员管理（包括安全管理制度类文档、记录表单类文档、系统相关人员）",2,1,false),
                generateTextAreaDataCell("经访谈负责人，未制定密码应用岗位责任制度。",2,2),
                generateEvidenceDataCell(2),
                generateComboBoxDataCell(2,4,rList,"不符合"),
                generateDataCell("0",2,5,false));
        rows.add(data);
    }

    private void generateTestData3(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("建立上岗人员培训制度",3,0),
                generateDataCell("人员管理（包括安全管理制度类文档、记录表单类文档、系统相关人员）",3,1,false),
                generateTextAreaDataCell("经访谈负责人，未制定上岗人员培训制度。",3,2),
                generateEvidenceDataCell(3),
                generateComboBoxDataCell(3,4,rList,"不符合"),
                generateDataCell("0",3,5,false));
        rows.add(data);
    }

    private void generateTestData4(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("建立关键岗位人员保密制度和调离制度",4,0),
                generateDataCell("人员管理（包括安全管理制度类文档、记录表单类文档、系统相关人员）",4,1,false),
                generateTextAreaDataCell("经访谈负责人和文档查看，制定了《计算机系统管理制度》，要求离岗人员必须严格办理离岗手续，明确其离岗后的保密义务，退还全部技术资料。",4,2),
                generateEvidenceDataCell(4),
                generateComboBoxDataCell(4,4,rList,"符合"),
                generateDataCell("1",4,5,false));
        rows.add(data);
    }

    private SpreadsheetCell generateTitleCell(String text,Integer rowIndex,Integer columnIndex){
        var cell = SpreadsheetCellType.STRING
                .createCell(rowIndex,columnIndex,1,1,text);
        cell.setEditable(false);
        cell.getStyleClass().add("header-column");
        cell.setWrapText(true);
        return cell;
    }

    private SpreadsheetCell generateDataCell(String text,Integer rowIndex,Integer columnIndex,Boolean edit){
        return generateDataCell(text,rowIndex,columnIndex,"data-cell",edit);
    }

    private SpreadsheetCell generateDataCell(String text,Integer rowIndex,Integer columnIndex,String css,Boolean edit){
        var cell = SpreadsheetCellType.STRING
                .createCell(rowIndex,columnIndex,1,1,text);
        cell.getStyleClass().add(css);
        cell.setEditable(edit);
        cell.setWrapText(true);
        return cell;
    }

    private SpreadsheetCell generateTextAreaDataCell(String text,Integer rowIndex,Integer columnIndex){
        var cell = PesSheetCellType.TEXT_AREA
                .createCell(rowIndex,columnIndex,1,1,text);
        cell.getStyleClass().addAll(Arrays.asList("data-cell-left","max-data-cell"));
        cell.setWrapText(true);
        return cell;
    }

    private SpreadsheetCell generateComboBoxDataCell(Integer rowIndex,Integer columnIndex,ObservableList<String> items,String value){
        var cell = PesSheetCellType.LIST(items)
                .createCell(rowIndex,columnIndex,1,1,value);
        cell.getStyleClass().add("data-cell");
        return cell;
    }


    private SpreadsheetCell generateEvidenceDataCell(Integer rowIndex){
        var cell = SpreadsheetCellType.STRING
                .createCell(rowIndex,3,1,1,null);
        FlowPane imgPane = new FlowPane();
        imgPane.getStyleClass().add("evidence-data-cell");
        imgPane.setOrientation(Orientation.HORIZONTAL);
        imgPane.setHgap(10);
        imgPane.setVgap(5);
        imgPane.getChildren().addAll(generateEvidenceLabel("证据文件1"),
                generateEvidenceLabel("证据文件2"),
                generateEvidenceLabel("证据文件3"),
                generateEvidenceLabel("证据文件4"));
        cell.setGraphic(imgPane);
        cell.getStyleClass().add("data-cell");
        cell.setEditable(false);
        return cell;
    }

    private Label generateEvidenceLabel(String fileName){
        var lbl = new Label(fileName);
        lbl.getStyleClass().add("evidence-name-label");
        return lbl;
    }
}